package com.atguigu.common.handler;

/**
 * ClassName: GlobalExceptionHandler
 * Package: com.atguigu.common.handler
 * Description:
 *
 * @Author 蹇泽阳
 * @Create 2023/11/13 17:48
 * @Version 1.0
 */

import com.atguigu.common.Result;
import com.atguigu.common.execption.GuiguException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 全局异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {
  
  @ExceptionHandler(Exception.class)
  @ResponseBody
  public Result error(Exception e) {
    e.printStackTrace(); // 输出异常信息
    return Result.fail();
  }
  
  @ExceptionHandler(ArithmeticException.class)
  @ResponseBody
  public Result error(ArithmeticException e) {
    e.printStackTrace();
    return Result.fail().message("执行了特定异常处理");
  }
  
  @ExceptionHandler(GuiguException.class)
  @ResponseBody
  public Result error(GuiguException e) {
    e.printStackTrace();
    return Result.fail().message(e.getMessage()).code(e.getCode());
  }
  
  /**
   * spring security异常
   *
   * @param e
   * @return
   */
  @ExceptionHandler(AccessDeniedException.class)
  @ResponseBody
  public Result error(AccessDeniedException e) throws AccessDeniedException {
    // return Result.build(null, ResultCodeEnum.PERMISSION);
    return Result.fail().code(205).message("没有操作权限");
  }
}
